package com.huawei.admins.platform.gateway.core.component.predicate.factory;

import com.huawei.admins.platform.gateway.core.component.predicate.AsyncPredicate;
import com.huawei.admins.platform.gateway.core.component.predicate.PredicateDefinition;
import com.huawei.admins.platform.gateway.core.config.GatewayProperties;
import com.huawei.admins.platform.gateway.core.util.NameUtils;
import org.springframework.util.Assert;
import org.springframework.web.server.ServerWebExchange;

import java.util.function.Predicate;

public interface RoutePredicateFactory {

    /**
     * Pattern key.
     */
    String PATTERN_KEY = "pattern";

    Predicate<ServerWebExchange> apply(PredicateDefinition definition, GatewayProperties properties);

    default AsyncPredicate<ServerWebExchange> applyAsync(PredicateDefinition definition, GatewayProperties properties) {
        Predicate<ServerWebExchange> predicate = apply(definition, properties);
        Assert.notNull(predicate, "predicate must not be null");
        return AsyncPredicate.from(predicate);
    }

    default String name() {
        return NameUtils.normalizeRoutePredicateName(getClass());
    }
}
